package com.example.airhockeytextured

import android.content.Context
import android.opengl.GLES20.*
import android.opengl.GLSurfaceView
import android.opengl.Matrix.*
import android.util.Log
import com.example.airhockeytextured.helper.ShaderHelper
import com.example.airhockeytextured.utils.LoggerConfig
import com.example.airhockeytextured.helper.MatrixHelper
import com.example.airhockeytextured.helper.TexturedHelper
import com.example.airhockeytextured.objects.Mallet
import com.example.airhockeytextured.objects.Table
import com.example.airhockeytextured.program.ColorShaderProgram
import com.example.airhockeytextured.program.TextureShaderProgram
import com.example.airhockeytextured.utils.OpenGLUtils
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

/**
 *  @author : BuildF
 *  date : 2020/7/9 15:56
 *  description :
 */
class AirHockeyRenderer constructor(context: Context) :
    GLSurfaceView.Renderer {
    private val context: Context by lazy { context }
    private var table: Table? = null
    private var mallet: Mallet? = null
    private var textureShaderProgram: TextureShaderProgram? = null
    private var colorShaderProgram: ColorShaderProgram? = null
    private var texture: Int = 0
    private val projectionMatrix = FloatArray(16)

    private val modelMatrix = FloatArray(16)


    override fun onDrawFrame(glUnused: GL10?) {
        // Clear the rendering surface.
        glClear(GL_COLOR_BUFFER_BIT)

        // Draw the table.
        textureShaderProgram?.usrProgram()
        textureShaderProgram?.setUniforms(projectionMatrix, texture)
        textureShaderProgram?.let {
            table?.bindData(it)
            table?.draw()
        }

        // Draw the first mallet.
        colorShaderProgram?.run {
            usrProgram()
            setUniforms(projectionMatrix)
            mallet?.bindData(this)
            mallet?.draw()
        }
    }

    override fun onSurfaceChanged(p0: GL10?, width: Int, height: Int) {
        Log.d("ljy", "onSurfaceChanged")
        glViewport(0, 0, width, height)
        MatrixHelper.persperctiveM(
            projectionMatrix,
            45f,
            width.toFloat() / height.toFloat(),
            1f,
            10f
        )
        setIdentityM(modelMatrix, 0)
        translateM(modelMatrix, 0, 0f, 0f, -3.0f)
        rotateM(modelMatrix, 0, -80f, 1f, 0f, 0f)
        val temp = FloatArray(16)
        multiplyMM(temp, 0, projectionMatrix, 0, modelMatrix, 0)
        System.arraycopy(temp, 0, projectionMatrix, 0, temp.size)

    }


    override fun onSurfaceCreated(p0: GL10?, p1: EGLConfig?) {
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f)
        table = Table()
        mallet = Mallet()
        textureShaderProgram =
            TextureShaderProgram(context, "texture_vertex_shader.glsl", "texture_fragment_shader.glsl")
        colorShaderProgram =
            ColorShaderProgram(context, "simple_vertex_shader.glsl", "simple_fragment_shader.glsl")
        texture = TexturedHelper.loadTextured(context, R.drawable.air_hockey_surface)
    }
}